﻿@using CSF.Security.BLL;
@using CSF.Security.Data;
@using CSF.Utility.Mvc;
@functions{  
    public string RenderRoleTree()
    {
        int i = 0;   //有根结点时
        var data = ViewBag.Permissions as IEnumerable<Permission>;
        var selectedOption = Model;
        var builder = new CSF.Utility.Mvc.TreeBuilder<Permission, int>(data,
                      p => p.Id,
                      p => p.ParentId ?? 0,
                      (p, isLeaf) =>
                      {
                          i++;
                          string childnames = "child" + i;
                          string s = "<span class=\"";
                          if (isLeaf)
                          {
                              s += "root" + i;

                          }
                          else
                          {
                              s += childnames;

                              s += "\" style=\"font-weight:bold; ";

                          }
                          s += "\"><input type=\"checkbox\" onclick=\"javascript:check('" + childnames + "');\" name=\"checkedall\" id=\"" + childnames + "\" value=\"" + p.Id + "\" ";
                          if (selectedOption.ContainsKey(p.Id))
                          {
                              s += "checked=\"checked\" ";
                          }

                          s += "/>";
                          s += p.Name1;
                          s += "</span>";
                          return s;

                      }, 0
                   );
        return builder.ToString(new { @class = "role_treeview" });

    }

}

    @Html.Raw(RenderRoleTree())

<script type="text/javascript">
    function check(e) {
        var parent = $("#" + e)[0].parentNode.parentNode.parentNode;
        $(parent).find(":checkbox").attr('checked', $("#" + e).attr('checked'));
    }
    $('.role_treeview').tree();
     
</script>
